<div class="layuimini-container layui-anim layui-anim-upbit">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">权限组名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input"
                                       id="name">
                                <input id="roleNewValue" name="roleNewValue" value="" hidden>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn" style="margin-left:10px;width:90px;height:38px;border:none;outline:none;border-radius:6px;background-color:#007bff;color:white"><i
                                    class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <br>
        <div class="layui-btn-container" style="position: relative;">
            <button type="submit" id="addGroup" class="layui-btn layui-btn-warm layui-btn-radiusadd-dept" lay-filter="data-add-btn"
                    style="width:124px;height:38px;background-color:#FFB800;border:none;outline:none;color:white"><i class="layui-icon layui-icon-form"></i>添加权限组</button>
        </div>
        <table id="authorityTable" lay-filter="currentTableFilter"></table>
        <!--<table id="tableSc" class="layui-table" lay-filter="tableSc" hidden></table>-->
        <div id="authorityPage"></div>
    </div>
</div>
<script type="text/html" id="currentTableBar">
    <a class="layui-btn layui-btn-xs layui-btn data-count-edit" lay-event="editPerson" id="editPerson">编辑成员</a>
    <a class="layui-btn layui-btn-xs layui-btn layui-btn-normal data-count-edit" lay-event="edit" id="edit">编辑权限</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger data-delete-btn"  lay-event="delete" id="delete">删除权限组</a>
</script>

<script>
    var tableBar = [
        { title: '序号', type: 'numbers', width: '10%', align: "center" },
        { field: 'name', title: '权限组名', width: '30%', align: "center" },
        { field: 'status', title: '状态', width: '30%', align: "center", templet: function (param) {
            if(param.status == '0'){
                return '<input type="checkbox" checked="" name="qiYong" lay-skin="switch" lay-text="启用|禁用" id="'+param.id+'" lay-filter="switchTest">'
            }else{
                return '<input type="checkbox" name="qiYong" lay-skin="switch" lay-text="启用|禁用" id="'+param.id+'" lay-filter="switchTest">'
            }
        }},
        { title: '操作', templet: '#currentTableBar', width: '30%', align: "center" }
    ]

    var initTableParams = {
        "name": "",
        "orderType": "name",
        "orderValue": "asc",
        "page": 1,
        "pageSize": 10
    }

    layuiEx.tableRender(
        "/sys-mgr/grouplist",
        'authorityTable',
        'authorityPage',
        tableBar,
        initTableParams
    );

    //检索事件
    layui.form.on('submit(data-search-btn)', function (params) {
        $('#name').val(params.field.name);
        layuiEx.tableRender(
            "/sys-mgr/grouplist", //按条件检索权限组
            'authorityTable',
            'authorityPage',
            tableBar,
            {
                "name": params.field.name,
                "orderType": "name",
                "orderValue": "asc",
                "page": 1,
                "pageSize": 10
            }
        );
        return false
    })

    //禁用/启用
    layui.form.on('switch(switchTest)',function (obj) {
        newID = obj.elem.id;
        newStatus = "1";
        if(obj.elem.checked){
            newStatus="0";
        }
        shared.ajaxPost('/sys-mgr/changeStatus', { "status": newStatus,"id":newID}, function callback(res) {
            if (res) {
                layer.msg(res, {
                    icon: 1,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                });
            }
            layuiEx.tableRender(
                "/sys-mgr/grouplist",//查询权限组列表
                'authorityTable',
                'authorityPage',
                tableBar,
                {
                    "name": $("#name").val(),
                    "status":"0",
                    "orderType": "name",
                    "orderValue": "asc",
                    "page": 1,
                    "pageSize": 10
                }
            );
        })
    })

    // 添加权限组按钮
    $("#addGroup").on("click", function (obj) {
        addOrEditAuthority("add")
    });

    // 监听编辑操作
    layui.table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;
        if (obj.event === 'edit') {
            addOrEditAuthority("edit",data) //编辑权限
        } else if (obj.event === 'delete') {
            layer.confirm('确定删除 ' + obj.data.name + " ?", function (index) {
                debugger
                deleteAuthority(data) //删除权限组
            })
        }else if(obj.event === 'editPerson'){
            editMember(data.id,data.name); //编辑成员
        }
    });

    // 新增编辑权限信息
    function addOrEditAuthority(addORedit,data){
        debugger
        var content = layui.miniPage.getHrefContent('mypages/usual/systemMgt/addAuthority.html');
        var openWH = layui.miniPage.getOpenWidthHeight();
        var title = ""
        if (addORedit=="add") {
            title = "添加权限组"
        }else{
            title = "编辑权限组"
        }
        //不能用 var 前缀
        data_authority = data
        var index = layer.open({
            title: title,
            type: 1,
            shade: 0.2,
            maxmin: true,
            shadeClose: true,
            offset: '100px',
            area : [ '500px', '300px' ],
            content: content,
            end: function () {
                debugger
                var trunToPage = 1
                if (addORedit != "add") {
                    trunToPage = $(".layui-laypage-em").next().html()
                };
                layuiEx.tableRender(
                    "/sys-mgr/grouplist",//查询权限组列表
                    'authorityTable',
                    'authorityPage',
                    tableBar,
                    {
                        "name": $('#name').val(),
                        "status":"0",
                        "orderType": "name",
                        "orderValue": "asc",
                        "page": trunToPage,
                        "pageSize": 10
                    }
                );
            }
        });
        $(window).on("resize", function () {
            layer.full(index);
        });
        return false;
    }

    //删除权限组
    function deleteAuthority(data){
        debugger
        shared.ajaxPost('/sys-mgr/deleteAuthority', { "role_id": data.id}, function callback(res) {
            debugger
            if (res) {
                layer.msg(res, {
                    icon: 1,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                })
            };
            layer.closeAll();
            layuiEx.tableRender(
                "/sys-mgr/grouplist",//查询权限组列表
                'authorityTable',
                'authorityPage',
                tableBar,
                {
                    "name": $('#name').val(),
                    "status":"0",
                    "orderType": "name",
                    "orderValue": "asc",
                    "page": 1,
                    "pageSize": 10
                }
            );
        })
    }

    //編輯成員
    function editMember(role,name){
        $("#roleNewValue").val(role);
        /*var content = layui.miniPage.getHrefContent('mypages/usual/systemMgt/chooseMember.html');*/
        var content = layui.miniPage.getHrefContent('mypages/usual/systemMgt/chooseMemberTwo.html');
        var openWH = layui.miniPage.getOpenWidthHeight();
        layui.use('layer',function () {
            layer.open({
                title: "编辑成员——"+name,
                type: 1,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                offset: '100px',
                /*area : [ '600px', '800px' ],*/
                area : [ '600px', '750px' ],
                content: content,
                success:function(){
                    $("#roleNewValue").val(role);
                },
                btn:['确定','取消'],
                yes:function(layero, index){
                    saveCheckedDatas(); //只有穿梭框样式调用了这个方法
                    layer.close(layer.index);
                },
                end: function () {
                    var trunToPage = 1
                    trunToPage = $(".layui-laypage-em").next().html()
                    layuiEx.tableRender(
                        "/sys-mgr/grouplist", //查询权限组列表
                        'authorityTable',
                        'authorityPage',
                        tableBar,
                        {
                            "name": $('#name').val(),
                            "status":"0",
                            "orderType": "name",
                            "orderValue": "asc",
                            "page": trunToPage,
                            "pageSize": 10
                        }
                    );
                }
            });
        })

        return false;
    }

    /*保存选择的成员*/
    function saveCheckedDatas(){
        debugger
        var checkedData = layui.transfer.getData("chooseMemSen"); //穿梭框右侧的数据
        shared.ajaxPost('/sys-mgr/saveCheckedDatasTwo', {"checkedData":checkedData, "role":$("#roleNewValue").val()},
                function callback(res){
                    if (res) {
                        layer.msg(res, {
                            icon: 1,
                            time: 1000 //2秒关闭（如果不配置，默认是3秒）
                        });
                    }
                }
        )
    }
</script>